import urlConfig from './config.js'
import common from './common.js'
import {
	userInfo
} from 'os'

const request = {}
const headers = {}

//基础请求方法
request.globalRequest = (url, method, data, dataType, power, loginMark = '') => {
	// 按照不同类型对数据加密
	switch (dataType) {
		case 'Image':
			data = {
				Info: common.imageRequestBuilder(data)
			}
			break
	}

	var rq = "";
	if (typeof data === 'string') {
		rq = data
	} else {
		rq = JSON.stringify(data)
	}
	var requestData = {
		token: uni.getStorageSync("userInfo").token,
		loginMark: loginMark ? loginMark : uni.getStorageSync('userInfo').loginMark,
		data: rq
	}
	var promise = new Promise(function(resolve, reject) { //, reject
		uni.request({
			url: urlConfig + url,
			method: method,
			data: requestData,
			//dataType: 'x-www-form-urlencoded',
			success: function(res) {				
				var r = res.data
				if (r.code == 200) {
					resolve(r.data);
				} else if (r.code == 410) {
					uni.showToast({
						title: r.info,
						icon: 'none'
					})
					uni.navigateTo({
						url: '/pages/login/login'
					})
				} else {					
					uni.showToast({
						title: r.info,
						icon: 'none'
					});
					reject(r);
				}

			},
			fail: function(res) {
				uni.showToast({
					title: '服务器错误',
					icon: 'none'
				})
				reject(res);
			}
		})
	})
	return promise;
}


export default request